package guiBeatporty;
import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.Action;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JButton;

import Controle.Controlador;
import Dominio.Usuario;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Login extends JInternalFrame {
	private JTextField txtNome;
	private JPasswordField txtSenha;
	
	private JRadioButton rdbtnUsurio; 
	private JRadioButton rdbtnArtista;
	private JButton btnCadastro;
	private JButton btnOk;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login frame = new Login();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Login() {
		setBounds(100, 100, 450, 300);
		
		JLabel lblLogin = new JLabel("Login");
		lblLogin.setBounds(191, 11, 43, 22);
		lblLogin.setFont(new Font("Tahoma", Font.PLAIN, 18));
		
		JLabel lblNome = new JLabel("Login:");
		lblNome.setBounds(96, 69, 52, 14);
		
		txtNome = new JTextField();
		txtNome.setBounds(153, 66, 142, 20);
		txtNome.setColumns(10);
		
		JLabel lblSenha = new JLabel("Senha:");
		lblSenha.setBounds(96, 100, 52, 14);
		
		txtSenha = new JPasswordField();
		txtSenha.setBounds(153, 97, 142, 20);
		
		btnCadastro = new JButton("Cadastro");
		btnCadastro.setBounds(139, 214, 142, 23);
		getContentPane().add(btnCadastro);
		btnCadastro.setEnabled(false);
		
		
		rdbtnUsurio = new JRadioButton("Usu\u00E1rio");
		rdbtnArtista = new JRadioButton("Artista");
		
		rdbtnUsurio.setBounds(182, 124, 89, 23);
		rdbtnArtista.setBounds(182, 150, 89, 23);
		
		rdbtnUsurio.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0){
				rdbtnArtista.setSelected(false);
				btnCadastro.setEnabled(true);
				btnOk.setEnabled(true);
				}			
		});
		
		rdbtnArtista.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0){
				rdbtnUsurio.setSelected(false);
				btnCadastro.setEnabled(true);
				btnOk.setEnabled(true);
				}
			
		});
		
		btnCadastro.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0){
				if(rdbtnUsurio.isSelected()){
					CadastroUsuario frameCadastroUsuario = new CadastroUsuario();
					MainFrame.addTelaDesk(frameCadastroUsuario);
					dispose();
				}else
				if(rdbtnArtista.isSelected()){
					CadastroArtista frameCadastroArtista = new CadastroArtista();
					MainFrame.addTelaDesk(frameCadastroArtista);
					dispose();
				}
			}
		});
			
		btnOk = new JButton("Ok");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(rdbtnUsurio.isSelected()){
					String nome = txtNome.getText();
					String senha = new String(txtSenha.getPassword());
					Controlador control = Controlador.getControlador();
					if(control.login(nome,senha)){
						control.setMainFrameUserInfo();
						MainFrame.resizeMainFrame(true);
						dispose();
					}
					else{
					}
				}
				else
				if(rdbtnArtista.isSelected()){
					String nome = txtNome.getText();
					String senha = new String(txtSenha.getPassword());
					Controlador control = Controlador.getControlador();
					if(control.loginArtista(nome,senha)){
						control.setMainFrameArtistInfo();
						dispose();
						MainFrame.resizeMainFrame(true);
					}
					else{
					}
				}
				
			}
		});
		btnOk.setBounds(176, 180, 80, 23);
		btnOk.setEnabled(false);
		getContentPane().setLayout(null);
		getContentPane().add(lblLogin);
		getContentPane().add(lblNome);
		getContentPane().add(txtNome);
		getContentPane().add(lblSenha);
		getContentPane().add(txtSenha);
		getContentPane().add(rdbtnUsurio);
		getContentPane().add(rdbtnArtista);
		getContentPane().add(btnOk);

	}
}
